package com.liuning.graduationdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuning.graduationdemo.entity.DoctorDepartment;
import com.liuning.graduationdemo.entity.User;
import com.liuning.graduationdemo.service.IDoctorDepartmentService;
import com.liuning.graduationdemo.service.IUserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import com.liuning.graduationdemo.common.Result;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pumpkin
 * @since 2022-04-22
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {

//    @Resource
//    private IDoctorService doctorService;

    @Resource
    private IUserService userService;

    @Resource
    private IDoctorDepartmentService doctorDepartmentService;

    /**
     * 保存或更新 医生信息
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        System.out.println("***********doctorDepartmentId:" + user.getDepartment().getId());
        System.out.println("***********doctorId:" + user.getId());
        //先插入数据，再根据username获取到新插入数据的id，根据id同步到关联表中
        //新增医生
        if (user.getId() == null) {
            //新增
            user.setPassword("888888");
            userService.saveOrUpdate(user);

            //查询新插入的医生id
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",user.getUsername());
            User addDoctor = userService.getOne(queryWrapper);
            Integer addDoctorId = addDoctor.getId();
            //同步到关联表中
            DoctorDepartment doctorDepartment = new DoctorDepartment();
            doctorDepartment.setDoctorId(addDoctorId);
            doctorDepartment.setDepartmentId(user.getDepartment().getId());

            //再进行插入操作
            doctorDepartmentService.save(doctorDepartment);
        }else {
            //更新医生表
            userService.saveOrUpdate(user);
            //更新关联表
            DoctorDepartment doctorDepartment = new DoctorDepartment();
            doctorDepartment.setDoctorId(user.getId());
            doctorDepartment.setDepartmentId(user.getDepartment().getId());
            QueryWrapper<DoctorDepartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("doctor_id",user.getId());
            doctorDepartmentService.update(doctorDepartment,queryWrapper);
        }
        return Result.success();
    }

    /**
     * 删除单条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.removeById(id);
        return Result.success();
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("role","ROLE_DOCTOR");
        return Result.success(userService.list(queryWrapper));
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }

    /**
     * 医生管理 分页查找所有数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String nickname,
                           @RequestParam(defaultValue = "") Integer deptId,
                           @RequestParam(defaultValue = "") String roleType) {
        return Result.success(userService.findDoctorPage(new Page<>(pageNum, pageSize),nickname,deptId,roleType));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        userService.removeBatchByIds(ids);
        return Result.success();
    }


}

